window.onload=function(){

	// 动画函数
	function animate(obj, target) {
		clearInterval(obj.timer)
		var speed = obj.offsetLeft < target ? 15 : -15
		obj.timer = setInterval(function() {
			var result = target - obj.offsetLeft
			obj.style.left = obj.offsetLeft + speed + 'px'
			if(Math.abs(result) <= Math.abs(speed)) {
				clearInterval(obj.timer)
				obj.style.left = target + 'px'
				console.log(1)
			}

		}, 2)
	}

	// 获取元素
	var box = document.getElementById('banner_slider_pics')
	var pics = box.children
	var picWidth = pics[0].offsetWidth
	var prev = document.getElementById('arrow_prev')
	var next = this.document.getElementById('arrow_next')

	// 无缝滚动。复制第一张到最后面
	var pic = pics[0].cloneNode(true)
	// 无缝滚动。复制最后一张到最前面
	var pic1 = pics[pics.length-1].cloneNode(true)
	
	box.appendChild(pic)
	box.insertBefore(pic1, pics[0] )

	var timer = setInterval(autoplay, 3000)
	var key = 0
	function autoplay() {
		key++
		if(key > pics.length - 1) {
			box.style.left = 0
			key = 1
		}
		animate(box, -key * picWidth)
	}

	next.onclick = function() {
		clearInterval(timer)
		key++
		if(key > pics.length - 1) {
			box.style.left = 0
			key = 1
		}
		animate(box, -key * picWidth)
		timer = setInterval(autoplay, 3000)
	}
	prev.onclick = function() {
		clearInterval(timer)
		key--
		if(key === 0) {
			box.style.left = -4 * picWidth + 'px'
			key = 3
		}
		animate(box, -key * picWidth)
		timer = setInterval(autoplay, 3000)
	}
}